<?php

    require_once dirname(dirname(__FILE__)) . '/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_affiliate.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_user.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_affiliate_register_form.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_bet_calculators.php';

    /**
     * User: Viet Anh
     * Date: 06/06/2014
     * Time: 11:54
     */
    class api_affiliate
    {

        public static function is_affiliate($user_id)
        {
            $dao_affiliate = new dao_affiliate();

            return $dao_affiliate->is_affiliate($user_id);
        }

        public static function approve_form($affiliate_form_id)
        {
            $dao_affiliate_register_form = new dao_affiliate_register_form();
            $form = $dao_affiliate_register_form->get_by_id($affiliate_form_id);
            $dao_user = new dao_user();
            $user_id = $form['user_id'];
            $dao_user->update_is_affiliate($user_id);
            $aff_commission = api_bet_calculators::get_affiliate_commission() * 100;
            $dao_user->update_affiliate_commission($user_id, $aff_commission);
            $dao_affiliate = new dao_affiliate();
            $dao_affiliate->register_affiliate($user_id);
            return $dao_affiliate_register_form->approve_form($affiliate_form_id);
        }
        
        public static function send_mail_for_affiliate_betting($affiliate_email, $affiliate_url, $bet_amount) {
            $email_sent_to = $affiliate_email;
            $message = "Currenly your affiliate bet for you with these information:\n "
                    . "- Your email: ".$affiliate_email." \n"
                    . "- Bet amount: $".$bet_amount."\n"
                    . "Please go to this link to register your account at Betting Websites and earn a lot of money:\n"
                    . $affiliate_url;
            mail($email_sent_to, "Your affiliate now bet for you  | Betting Websites", $message);
        }
        
        public static function get_affiliate_form_by_id($affiliate_form_id)
        {
            $dao_affiliate_register_form = new dao_affiliate_register_form();

            return $dao_affiliate_register_form->get_by_id($affiliate_form_id);
        }
        
        public static function delete_form($affiliate_form_id)
        {
            $dao_affiliate_register_form = new dao_affiliate_register_form();

            return $dao_affiliate_register_form->delete($affiliate_form_id);
        }
        
        public static function get_list_client($user_id)
        {
            $dao_affiliate = new dao_affiliate();

            return $dao_affiliate->get_client_list($user_id);
        }

        public static function register_affiliate($user_id, $question_1, $question_2,
                $question_3, $question_4, $question_5)
        {
            $dao_affiliate_register_form = new dao_affiliate_register_form();
            return $dao_affiliate_register_form->submit_form($user_id, $question_1, $question_2,
                    $question_3, $question_4, $question_5);
        }

        public static function get_all_affiliate_form_need_approve()
        {
            $dao_affiliate_register_form = new dao_affiliate_register_form();
            return $dao_affiliate_register_form->get_all();
        }
        
        public static function switch_aff_priviledge($aff_id, $aff_priviledge)
        {
            $dao_affiliate = new dao_affiliate();

            return $dao_affiliate->switch_aff_priviledge($aff_id, $aff_priviledge);
        }

        public static function get_aff_priviledge($aff_id)
        {
            $dao_affiliate = new dao_affiliate();

            return $dao_affiliate->get_aff_priviledge($aff_id);
        }

        public static function dehash_aff_id($hash_id)
        {
            $dao_user = new dao_user();
            $users    = $dao_user->get_all();
            foreach ($users as $user) {
                if (md5($user['id']) == $hash_id) {
                    return $user['id'];
                }
            }
        }

        public static function validate_affiliate_form_fields(
                $question_1, $question_2, $question_3, $question_4,
                $question_5) {
            $error_show = "";
            if (empty($question_1) || empty($question_2) || empty($question_3)
                    || empty($question_4) || empty($question_5)) {
                $error_show .= "Please fill enough question to complete.";
            }
            return $error_show;
        }
        
        public static function get_aff_priviledges()
        {
            $dao_affiliate = new dao_affiliate();

            return $dao_affiliate->get_aff_priviledges();
        }
    }